package com.xys.auth.mapper;

import com.xys.auth.bean.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {

    @Select("select * from users where username = #{username}")
    @Results({
            @Result(id=true,column = "id",property = "id"),
            @Result(column = "email",property = "email"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "phone_num",property = "phoneNum"),
            @Result(column = "status",property = "status"),

            @Result(column = "id",property = "roles",
                    many=@Many(select="com.xys.auth.mapper.RoleMapper.findByUserId")
            )
    })
    UserInfo findByUsername(@Param("username") String username);

    @Select("select * from users")
    List<UserInfo> findAll();

    @Update("insert into users values(#{id},#{email},#{username},#{password},#{phoneNum},#{status})")
    int save(UserInfo userInfo);

    @Select("select * from users where id =#{id}")
    @Results({
            @Result(id=true,column = "id",property = "id"),
            @Result(column = "email",property = "email"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "phone_num",property = "phoneNum"),
            @Result(column = "status",property = "status"),

            @Result(column = "id",property = "roles",
                    many=@Many(select="com.xys.auth.mapper.RoleMapper.findByUserId")
            )
    })
    UserInfo findById(String id);

    @Insert("insert into users_role values(#{userId},#{roleId})")
    int addRoleToUser(@Param("userId") String userId,@Param("roleId") String id);
}
